package cn.edu.ccut.refactor;

import org.eclipse.ltk.core.refactoring.Refactoring;
import org.eclipse.ltk.core.refactoring.RefactoringStatus;
import org.eclipse.ltk.core.refactoring.participants.RefactoringProcessor;
import org.eclipse.ltk.core.refactoring.participants.RenameRefactoring;
import org.eclipse.ltk.ui.refactoring.RefactoringWizard;

/**
 * 
 * @author Bai Yucheng 2008-1-8
 * 
 */
public class RenameRefactoringWizard extends RefactoringWizard {
	Refactoring fRefactoring;
	String newName;

	public RenameRefactoringWizard(Refactoring refactoring, int i) {
		super(refactoring, i);
		setDefaultPageTitle(Messages.getString("RenameRefactoringWizard.0")); //$NON-NLS-1$
		fRefactoring = refactoring;
	}

	protected void addUserInputPages() {
		RefactoringProcessor refactoringprocessor = ((RenameRefactoring) getRefactoring()).getProcessor();
		String s = ((AbstractRenameProcessor) refactoringprocessor).getCurrentElementName();
		String s1 = s.substring(0, s.lastIndexOf(".")); //$NON-NLS-1$
		RenameInputWizardPage renameinputwizardpage = createInputPage("", s1); //$NON-NLS-1$
		addPage(renameinputwizardpage);
	}

	protected RenameInputWizardPage createInputPage(String s, String s1) {
		return new RenameInputWizardPage(s, true, s1) {

			protected RefactoringStatus validateTextField(String s2) {
				return validateNewName(s2);
			}

		};
	}

	protected RefactoringStatus validateNewName(String s) {
		RefactoringStatus status = null;
		try {
			RefactoringProcessor refactoringprocessor;
			refactoringprocessor = ((RenameRefactoring) getRefactoring()).getProcessor();
			((AbstractRenameProcessor) refactoringprocessor).setNewElementName(s);
			status = ((AbstractRenameProcessor) refactoringprocessor).checkNewElementName(s);
			setNewName(s);
		} catch (Exception e) {
		}
		return status;
	}
	
	@Override
	public boolean performFinish() {
		try {
			super.performFinish();	
		} catch (Exception e) {
		}
		return true;
	}

	public String getNewName() {
		return newName;
	}

	public void setNewName(String newName) {
		this.newName = newName;
	}
	
//	@Override
//	public boolean canFinish() {
//		if(RenameInputWizardPage.flag){
//			return super.canFinish();
//		}else{
//			return false;
//		}
//	}

}
